﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Config_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            BindData()
        End If
    End Sub
    Private Sub BindData()
        Using data As New VSEntities
            Dim _Huyen = (From h In data.Provinces Where h.ParentId = 171 Select h).ToList
            With ddlHuyen
                .DataSource = _Huyen
                .DataTextField = "Title"
                .DataValueField = "Id"
                .DataBind()
                .Items.Insert(0, New ListItem("---Chọn---", ""))
            End With
        End Using
    End Sub
    Protected Function Save() As Boolean
        Using data As New VSEntities

            Dim _chkHuyen = (From j In data.Configs Where j.ProvinceId = ddlHuyen.SelectedValue Select j).FirstOrDefault
            If _chkHuyen Is Nothing Then

                Dim p As New VSModel.Config
                Try
                    p.ProvinceId = ddlHuyen.SelectedValue
                    p.BuoiTraHoSo = txtBuoitrahoso.Text.Trim()
                    Dim _IsBangHieu = chkIsBangHieu.Checked
                    If _IsBangHieu Then
                        p.Ref1 = 1
                    Else
                        p.Ref1 = 0
                    End If
                    p.Ref2 = txtRef2.Text.Trim()
                    Dim _IsGhiChuRamdom = chkRef3.Checked
                    If _IsGhiChuRamdom Then
                        p.Ref3 = 1
                    Else
                        p.Ref3 = 0
                    End If
                    data.Configs.AddObject(p)
                    data.SaveChanges()
                    'Insert_App_Log("Insert  Config:" & txtTitle.Text.Trim & "", Function_Name.Config, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Return True
                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Return False
                End Try
                Return True
            End If
            Excute_Javascript("Alertbox('Huyện đã tồn tại !.');", Me.Page, True)
            Return False
        End Using
    End Function
    Protected Sub ResetControl()
        ddlHuyen.SelectedValue = ""
        txtBuoitrahoso.Text = ""
        chkIsBangHieu.Checked = False
        txtRef2.Text = ""
        chkRef3.Checked = False
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Save() Then
            Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Config/List.aspx';", Me.Page, True)
        Else
            Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
        End If
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ResetControl()
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
